package karileet.meeting.profile.service;

import karileet.meeting.profile.MainActivity;
import karileet.meeting.profile.MeetingProfile;
import karileet.meeting.profile.MeetingProfileWidgetProvider;
import karileet.meeting.profile.ProfileState;
import karileet.meeting.profile.R;
import karileet.meeting.profile.R.drawable;
import karileet.meeting.profile.R.id;
import karileet.meeting.profile.R.layout;
import android.app.Service;
import android.appwidget.AppWidgetManager;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.widget.RemoteViews;

public class ActivationService extends Service {
	 
	   @Override
	   public void onCreate() {
	   }
	 
	   @Override
	   public IBinder onBind(Intent intent) {
	   return null;
	   }
	 
	   @Override
	   public void onDestroy() {
	   super.onDestroy();
	   }
	 
	   @Override
	   public int onStartCommand(Intent intent, int flags, int startId) {
		   super.onStartCommand(intent, flags, startId);
		   if (intent != null) {
				RemoteViews views = new RemoteViews(getApplicationContext()
						.getPackageName(), R.layout.meetingprofilewidget);
				Bundle extras = intent.getExtras();
				String type = extras.getString("scheduleType");
				ProfileState ps = new ProfileState(getApplicationContext());
				if (type.equals("start") && ps.isScheduleStartEnabled() && !ps.isProfileEnabled()) {
				    MeetingProfile mp = new MeetingProfile(getApplicationContext());
				    mp.enable(ps);
				    views.setImageViewResource(R.id.button1, R.drawable.mpbtn3_down);
				    Log.d(MainActivity.APP, "Enabling profile");
			    	   AppWidgetManager manager = AppWidgetManager.getInstance(getApplicationContext());
					   manager.updateAppWidget(manager.getAppWidgetIds(new ComponentName(getApplicationContext(), MeetingProfileWidgetProvider.class)), views);
				}
		   } else {
			   Log.d(MainActivity.APP, "Skiped start command, since intent was null");
		   }
		   return START_STICKY;
	   }

	}